package net.intellect.collectPro.businessLogic.payments;

import java.util.List;
import java.util.logging.Logger;

import net.intellect.force.ClientSalesforce;

import com.sforce.soap.enterprise.sobject.CP_Payment__c;
import com.sforce.soap.enterprise.sobject.Past_Due__c;

public class BasePaymentMethod  implements PaymentMethodProcess{

	protected static Logger logger = Logger.getLogger("payments");
	
	protected static final String CURRENCY_ISO_CODE_USD = "USD"; //$NON-NLS-1$	

	/**
	 * Default PaymentMethod (Interest to the PastDue )
	 *
	 * @param past_due pastDue
	 * @param payment Payments
	 * @throws Exception
	 */
	public void apply(Past_Due__c past_due, List<CP_Payment__c> paymentList) throws Exception {

	}
	

	/**
	 * Add and update the Payments's amounts
	 * @param past_due pastDue
	 * @param payment Payments
	 * @throws Exception
	 */
	protected void consolidatedPayments(Past_Due__c past_due, List<CP_Payment__c> paymentList)  throws Exception{
		Double localAmount = 0.0;
		Double usdAmount = 0.0;

		if (null != paymentList){			
			for (CP_Payment__c payment:paymentList){
				if (CURRENCY_ISO_CODE_USD.equals(payment.getCurrencyIsoCode())){
					usdAmount += payment.getAmount__c();
				}else{
					localAmount += payment.getAmount__c();
				}
				
				//Process the payment
				if (payment.getApplied__c() == false){
					CP_Payment__c paymentUpd = new CP_Payment__c();
					paymentUpd.setId(payment.getId());
					paymentUpd.setApplied__c(true);
					ClientSalesforce.update(paymentUpd);
				}
			}
			past_due.setPast_Due_Amount_Local__c(localAmount);
			past_due.setPast_Due_Amount_USD__c(usdAmount);
			
			//Fix NAN
			if (Double.isNaN(past_due.getMinimum_Payment_USD__c())){
				past_due.setMinimum_Payment_USD__c(0);
			}
			
			if (Double.isNaN(past_due.getMinimum_Payment_Local__c())){
				past_due.setMinimum_Payment_Local__c(0);
			}

		}
	}
	
}
